﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class WeiXinMenu
    {
        public int Add(Chain.Model.WeiXinMenu model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into WeiXinMenu(");
            builder.Append("MenuName,MenuType,MenuKey,MenuUrl,parentMenuID)");
            builder.Append(" values (");
            builder.Append("@MenuName,@MenuType,@MenuKey,@MenuUrl,@parentMenuID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MenuName", SqlDbType.NVarChar, 50), new SqlParameter("@MenuType", SqlDbType.Int, 4), new SqlParameter("@MenuKey", SqlDbType.NVarChar, 0x80), new SqlParameter("@MenuUrl", SqlDbType.NVarChar, 0x100), new SqlParameter("@parentMenuID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MenuName;
            cmdParms[1].Value = model.MenuType;
            cmdParms[2].Value = model.MenuKey;
            cmdParms[3].Value = model.MenuUrl;
            cmdParms[4].Value = model.parentMenuID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int MenuID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from WeiXinMenu ");
            builder.Append(" where MenuID=@MenuID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MenuID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MenuID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string MenuIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from WeiXinMenu ");
            builder.Append(" where MenuID in (" + MenuIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int MenuID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from WeiXinMenu");
            builder.Append(" where MenuID=@MenuID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MenuID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MenuID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MenuID,MenuName,MenuType,MenuKey,MenuUrl,parentMenuID ");
            builder.Append(" FROM WeiXinMenu ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" MenuID,MenuName,MenuType,MenuKey,MenuUrl,parentMenuID ");
            builder.Append(" FROM WeiXinMenu ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.MenuID desc");
            }
            builder.Append(")AS Row, T.*  from WeiXinMenu T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetMenuAllInfo()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MenuID,MenuName,MenuType,MenuKey,MenuUrl,parentMenuID ");
            builder.Append(",(select count(*) from WeiXinMenu as WeiXinMenuChild where WeiXinMenuChild.parentMenuID=WeiXinMenuParent.MenuID) as childNum ");
            builder.Append("from WeiXinMenu as WeiXinMenuParent");
            builder.Append(";select count(*) from WeiXinMenu where parentMenuID=0");
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetMenuAllInfo2()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select *,(select count(*) from WeiXinMenu where ParentMenuID=1) as childNum from WeiXinMenu  where MenuID=1 ");
            builder.Append("union all ");
            builder.Append("select *,0 as childNum from WeiXinMenu where ParentMenuID=1 ");
            builder.Append("union all ");
            builder.Append("select *,(select count(*) from WeiXinMenu where ParentMenuID=2) as childNum from WeiXinMenu  where MenuID=2 ");
            builder.Append("union all ");
            builder.Append("select *,0 as childNum from WeiXinMenu where ParentMenuID=2 ");
            builder.Append("union all ");
            builder.Append("select *,(select count(*) from WeiXinMenu where ParentMenuID=3) as childNum from WeiXinMenu  where MenuID=3 ");
            builder.Append("union all ");
            builder.Append("select *,0 as childNum from WeiXinMenu where ParentMenuID=3 ");
            builder.Append(";select count(*) from WeiXinMenu where parentMenuID=0 ");
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetMenuChildInfo(int parentMenuID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MenuID,MenuName,MenuType,MenuKey,MenuUrl,parentMenuID ");
            builder.Append("from WeiXinMenu as WeiXinMenuParent where parentMenuID=@parentMenuID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@parentMenuID", SqlDbType.Int, 4) };
            cmdParms[0].Value = parentMenuID;
            return DbHelperSQL.Query(builder.ToString(), cmdParms);
        }

        public DataSet GetMenuParentInfo()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MenuID,MenuName,MenuType,MenuKey,MenuUrl,parentMenuID ");
            builder.Append(",(select count(*) from WeiXinMenu as WeiXinMenuChild where WeiXinMenuChild.parentMenuID=WeiXinMenuParent.MenuID) as childNum ");
            builder.Append("from WeiXinMenu as WeiXinMenuParent where parentMenuID=0");
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.WeiXinMenu GetModel(int MenuID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 MenuID,MenuName,MenuType,MenuKey,MenuUrl,parentMenuID from WeiXinMenu ");
            builder.Append(" where MenuID=@MenuID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MenuID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MenuID;
            Chain.Model.WeiXinMenu menu = new Chain.Model.WeiXinMenu();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["MenuID"] != null) && (set.Tables[0].Rows[0]["MenuID"].ToString() != ""))
            {
                menu.MenuID = int.Parse(set.Tables[0].Rows[0]["MenuID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MenuName"] != null) && (set.Tables[0].Rows[0]["MenuName"].ToString() != ""))
            {
                menu.MenuName = set.Tables[0].Rows[0]["MenuName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MenuType"] != null) && (set.Tables[0].Rows[0]["MenuType"].ToString() != ""))
            {
                menu.MenuType = int.Parse(set.Tables[0].Rows[0]["MenuType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MenuKey"] != null) && (set.Tables[0].Rows[0]["MenuKey"].ToString() != ""))
            {
                menu.MenuKey = set.Tables[0].Rows[0]["MenuKey"].ToString();
            }
            if ((set.Tables[0].Rows[0]["MenuUrl"] != null) && (set.Tables[0].Rows[0]["MenuUrl"].ToString() != ""))
            {
                menu.MenuUrl = set.Tables[0].Rows[0]["MenuUrl"].ToString();
            }
            if ((set.Tables[0].Rows[0]["parentMenuID"] != null) && (set.Tables[0].Rows[0]["parentMenuID"].ToString() != ""))
            {
                menu.parentMenuID = int.Parse(set.Tables[0].Rows[0]["parentMenuID"].ToString());
            }
            return menu;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM WeiXinMenu ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int GetUseCountByRuleID(int RuleID)
        {
            string sQLString = " select count(*) from WeiXinRule,WeiXinMenu where WeiXinRule.RuleNUmber=WeiXinMenu.MenuKey and WeiXinRule.RuleID=@RuleID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@RuleID", SqlDbType.Int, 4) };
            cmdParms[0].Value = RuleID;
            object single = DbHelperSQL.GetSingle(sQLString, cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.WeiXinMenu model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update WeiXinMenu set ");
            builder.Append("MenuName=@MenuName,");
            builder.Append("MenuType=@MenuType,");
            builder.Append("MenuKey=@MenuKey,");
            builder.Append("MenuUrl=@MenuUrl,");
            builder.Append("parentMenuID=@parentMenuID");
            builder.Append(" where MenuID=@MenuID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MenuName", SqlDbType.NVarChar, 50), new SqlParameter("@MenuType", SqlDbType.Int, 4), new SqlParameter("@MenuKey", SqlDbType.NVarChar, 0x80), new SqlParameter("@MenuUrl", SqlDbType.NVarChar, 0x100), new SqlParameter("@parentMenuID", SqlDbType.Int, 4), new SqlParameter("@MenuID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MenuName;
            cmdParms[1].Value = model.MenuType;
            cmdParms[2].Value = model.MenuKey;
            cmdParms[3].Value = model.MenuUrl;
            cmdParms[4].Value = model.parentMenuID;
            cmdParms[5].Value = model.MenuID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public int UpdateMenuKey(string OldMenuKey, string NewMenuKey)
        {
            string sQLString = "update WeiXinMenu set MenuKey=@NewMenuKey where MenuKey=@OldMenuKey";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OldMenuKey", SqlDbType.NVarChar, 0x80), new SqlParameter("@NewMenuKey", SqlDbType.NVarChar, 0x80) };
            cmdParms[0].Value = OldMenuKey;
            cmdParms[1].Value = NewMenuKey;
            return DbHelperSQL.ExecuteSql(sQLString, cmdParms);
        }
    }
}

